package com.exp.zhiwendemo;

import java.util.List;

import com.dnake.v710.MessageLayer;
import com.dnake.v710.MessageProcessor;
import com.exp.zhiwendemo.widget.Button2;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SecondActivity extends Activity implements MessageProcessor {

	public static SecondActivity handle = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.second);
		
		handle = this;
		
		Button2 b2 = (Button2)findViewById(R.id.main_settings);
		b2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(SecondActivity.this, SetupLabel.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
			}
		});
		
		Button b = (Button)findViewById(R.id.btn_start);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				MessageLayer.zfm_led_onoff(1);
			}
		});
		
		b = (Button)findViewById(R.id.btn_stop);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				MessageLayer.zfm_led_onoff(0);
			}
		});
		
		MessageLayer.start(this);
		MessageLayer.setMessageProcessor(this);
	}
	
	private String getSetpDesc(String step) {
		String s = "";
		
		if (step.equals("ReadConList"))
			s = "分配储存位置";
		else if (step.equals("OpenLed"))
			s = "打开指纹灯";
		else if (step.equals("CloseLed"))
			s = "关闭指纹灯";
		else if (step.equals("GenImg"))
			s = "采集图像中";
		else if (step.equals("Img2Tz"))
			s = "图像转特征";
		else if (step.equals("RegModel"))
			s = "合成模板中";
		else if (step.equals("LoadChar"))
			s = "加载模板中";
		else if (step.equals("UpChar"))
			s = "上传特征或模板中";
		else if (step.equals("DownChar"))
			s = "下载特征或模板中";
		else if (step.equals("DelChar"))
			s = "删除模板中";
		else if (step.equals("Lssued"))
			s = "进入下载模式";
		else if (step.equals("Store"))
			s = "存储模板中";
		else if (step.equals("Match"))
			s = "指纹匹配中";
		else if (step.equals("Reset"))
			s = "指纹模块重置中";
		return s;
	}
	
	private short[] mData = new short[768];  //存储指纹特征数据
	
	public void key_back() {
		ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasks = am.getRunningTasks(1);
		String className = tasks.get(0).topActivity.getClassName();
		if (className.equals("com.dnake.desktop.SetupLabel")) {
			if (SetupLabel.handler != null)
				SetupLabel.handler.finish();
		} else if (className.equals("com.dnake.desktop.AppsLabel")) {
			if (AppsLabel.handler != null)
				AppsLabel.handler.finish();
		}
	}

	@Override
	public void processZfmResult(int code, int seq) {
		TextView tv = (TextView)findViewById(R.id.tv_status);
		
		switch (code) {
		case 0x00:
			tv.setText("成功");
			break;
			
		case 0x01:
			tv.setText("收包有错");
			break;
			
		case 0x02:
			tv.setText("传感器上无指纹");
			break;
			
		case 0x03:
			tv.setText("录入不成功");
			break;
			
		case 0x06:
			tv.setText("图像太乱，失败");
			break;
			
		case 0x07:
			tv.setText("特征点太少，失败");
			break;
	
		case 0x15:
			tv.setText("图像缓冲区没有有效原始图而生不成图像");
			break;				
			
		default:
			break;
		}
	}

	@Override
	public void processZfmStep(String step) {
		TextView tv = (TextView)findViewById(R.id.tv_status);
		tv.setText(getSetpDesc(step));
	}

	@Override
	public void processZfmFinger() {
		MessageLayer.zfm_char_upload();
	}

	@Override
	public void processZfmData(String d) {
		if (!d.isEmpty()) {
			Log.d("com.dnake.desktop", "zfmdata "+d.length());
			byte[] b = d.getBytes();
			if (b.length == 1024 || b.length == 1536) {
				int idx = 0;
				for (int i=0; i<b.length; i+=2) {
					mData[idx++] = (short) (((b[i]<<4)&0xF0)+(b[i+1]&0x0F));
					System.out.format("0x%02x, ", mData[idx-1]);
				}
				System.out.print('\n');
			}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK)
			return false;	
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void processCardData(String data) {
		// TODO Auto-generated method stub
		TextView tv = (TextView)findViewById(R.id.tv_brush_card);
		tv.setText(data);
	}
}
